package com.ruoyi.biz.card.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.ruoyi.biz.card.domain.CardInfo;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.parameter.AddressType;
import ezvcard.property.Address;
import ezvcard.property.Organization;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class VCardUtils {
    public String generateVCardString(CardInfo cardInfo) throws IOException {
        VCard vcard = new VCard();

        // 姓名
        if (cardInfo.getName() != null && !cardInfo.getName().isEmpty()) {
            vcard.setFormattedName(cardInfo.getName());
        }
        // 电话
        if (cardInfo.getMoblePhone() != null && !cardInfo.getMoblePhone().isEmpty()) {
            vcard.addTelephoneNumber(cardInfo.getMoblePhone());
        }
        // 工作地址
        if (cardInfo.getCompany() != null && !cardInfo.getCompany().isEmpty()) {
            Address workAddress = new Address();
            workAddress.setStreetAddress(cardInfo.getWorkAddress());
            workAddress.getTypes().add(AddressType.WORK);

            vcard.addAddress(workAddress);
        }
        // 邮箱
        if (cardInfo.getEmail() != null && !cardInfo.getEmail().isEmpty()) {
            vcard.addEmail(cardInfo.getEmail());
        }
        // 职位
        if (cardInfo.getPosition() != null && !cardInfo.getPosition().isEmpty()) {
            Organization organization = new Organization();
            organization.getValues().add(cardInfo.getCompany());
            vcard.addOrganization(organization);
        }

        // 头像
//        if (cardInfo.getHeadImg() != null && !cardInfo.getHeadImg().isEmpty()) {
//            byte[] imageBytes = Files.readAllBytes(Paths.get(cardInfo.getHeadImg()));
//            Photo photo = new Photo(imageBytes, ImageType.JPEG);
//            vcard.addPhoto(photo);
//        }

        // 输出为字符串
        String vcardString = Ezvcard.write(vcard).version(ezvcard.VCardVersion.V3_0).go();

        // 打印出 vcard 字符串
        System.out.println(vcardString);

        return vcardString;
    }
    public String generateVCardQrBase64(String vcardText) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        // ✅ 关键点：设置字符集为 UTF-8
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        // ✅ 将 hints 传入 encode 方法
        BitMatrix bitMatrix = qrCodeWriter.encode(vcardText, BarcodeFormat.QR_CODE, 300, 300, hints);

        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", outputStream);
        byte[] imageBytes = outputStream.toByteArray();

        return "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);
    }


}
